<template>
  <view>
    <!-- 头部导航栏 -->
    <view class="top-navigate" style="padding: 53px 10px 20px 10px;">
      <view class="content cuIcon-back text-xl " @click="goBack">
      </view>
      <view class="text-xl top-navigate-title">支付订单</view>
    </view>
    <!-- 订单剩余时间 -->
    <view class="remain-time text-df text-center">
      交易剩余时间 {{ minutes }} : {{ seconds }}
    </view>
    <!-- 订单费用 -->
    <view class="order-price text-sl text-center">
      ￥{{price}}
    </view>
    <!-- 支付方式 -->
    <view class="">
    </view>
    <!-- 确认下单按钮 -->
    <view class="confirm-pay-order">
      <button class="cu-btn round bg-orange shadow lg flex" style="margin: 15px auto;width: 95%;"
        @click="confirmPayOrder">
        确认支付
      </button>
    </view>
  </view>
</template>

<script>
  import getCurrentDateTime from '../../utils/currentTime.js'
  const db = uniCloud.database();
  export default {
    name: "pay-order",
    props: ['orderId', 'price'],
    data() {
      return {
        time15: '15:00',
        minutes: 15,
        seconds: 0,
        timer: null
      };
    },
    mounted() {
      this.timer = setInterval(() => {
        if (this.seconds > 0) {
          this.seconds--;
        } else {
          if (this.minutes === 0) {
            clearInterval(this.timer);
            // 倒计时结束的操作
          } else {
            this.minutes--;
            this.seconds = 59;
          }
        }
      }, 1000);
    },
    beforeDestroy() {
      clearInterval(this.timer);
    },
    methods: {
      //返回订单页面
      goBack() {
        this.$parent.payOrderShow = false
      },
      //支付订单
      async confirmPayOrder() {
        const {
          result: res
        } = await db.collection("my_order").where({
          _id: this.orderId
        }).update({
          state: '待接单',
          price:this.price
        })
        console.log('res：', res);
        if (res.updated === 1) {
          const currentDateTime = getCurrentDateTime();
          console.log(currentDateTime); // 输出类似于 "2024-02-20 15:30:45"
          const {
            result: res
          } = await db.collection("my_order").where({
            _id: this.orderId
          }).update({
            payTime: currentDateTime
          });
          
          uni.navigateTo({
            url: '/subpkg/success/success?orderId=' + this.orderId+'&price='+this.price
          })
          return uni.$showMsg('支付成功！')
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
  .top-navigate {
    position: relative;
    display: flex;
    justify-content: left;
    font-weight: bold;
    align-items: center;

    .top-navigate-title {
      margin: 0 auto;
    }
  }

  .confirm-pay-order {
    position: relative;
    bottom: 0;
  }
</style>